//
//  DUAlert.m
//  APP
//
//  Created by duyujin1 on 2021/8/25.
//

#import "DUAlert.h"
#import <UIKit/UIKit.h>

@implementation DUAlert

+(void)showAlertWithTitle:(NSString *)title detailIfon:(NSString *)detailIfon confirmHandle:(DUAlertConfirmHandle)confirmHandle cancelHandle:(DUAlertCancelHandle)cancelHandle{
    UIAlertController * alertVC=[UIAlertController alertControllerWithTitle:title message:detailIfon preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(confirmHandle){
            confirmHandle();
        }
    }];
    UIAlertAction * cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(cancelHandle){
            cancelHandle();
        }
    }];
    [alertVC addAction:confirmAction];
    [alertVC addAction:cancelAction];
    [[DUAlert currentViewController] presentViewController:alertVC animated:YES completion:nil];
}

+(UIViewController *)currentViewController{
    UIWindow * window=[[UIApplication sharedApplication] delegate].window;
    UIViewController * presentVC=[[window rootViewController] presentedViewController];
    if(presentVC){
        //当前视图不在navigationController的堆栈中，且当前视图也是模态弹出的
        return presentVC;
    }
    else{
        //当前页面在navigationController的堆栈中
        return window.rootViewController;
    }
}

@end
